package com.itheima.test1;

import java.util.ArrayList;
import java.util.Collection;

public class MyCollectionDemo2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<String>();
        //添加元素  boolean add(E e);
        collection.add("aaa");
        collection.add("bbb");
        collection.add("ccc");
        collection.add("ddd");
        collection.add("eee");
        System.out.println(collection);
        //删除元素  boolean remove(Object o);
        //如果删除成功,返回true
        //如果删除失败,返回false
        boolean aaa = collection.remove("aaa");
        boolean eee = collection.remove("eee");
        System.out.println(aaa);
        System.out.println(eee);
        //根据条件删除  boolean removeIf(Object o);
        //removeIf底层会遍历集合,得到集合中的每一个元素
        //s依次表示集合中的每一个元素
        //就会把每一个元素都到lambda表达式中判断一下
        //如果返回的是true,则删除
        //如果返回的是false,就保留不删除
        //boolean test(T t);
        boolean b = collection.removeIf(
                (String s) -> {
                    return s.length() == 3;
                }
        );
        System.out.println(b);
        System.out.println(collection);
        //删除元素    void clear();
        collection.clear();
        System.out.println(collection);
        //判断集合中是否有指定的元素   boolean contains(Object o);
        boolean aaa1 = collection.contains("aaa");
        boolean aaa2 = collection.contains("a");
        System.out.println(aaa1);
        System.out.println(aaa2);
        //判断集合是否为空   boolean isEmpty();
        boolean empty = collection.isEmpty();
        System.out.println(empty);
        //集合的长度(元素的个数)      int size();
        int size = collection.size();
        System.out.println(size);
    }
}
